package com.topscomm.gpm.script;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboOrgEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.cbo.service.impl.CboOrgService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.mis.constant.MisSystemConst;
import com.topscomm.mis.mobile.message.service.SendMessageService;
import com.topscomm.pub.util.*;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.GpmSystemConst;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmWfFormEntity;
import com.topscomm.gpm.service.impl.GpmProjectHistoryService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.gpm.service.impl.GpmWfFormService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.ParameterCache;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description TODO
 * @Classname SdProjectScript
 * @Date 2021/12/8
 * @Created by jiangtao
 */
public class GpmProjectScript extends GpmScript {

	@Transactional(rollbackFor = Exception.class)
	public void projectUpdate(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil
				.getBean("gpmProjectService");
		// 查找审批实体
		GpmWfFormService gpmWfFormService = (GpmWfFormService)SpringUtil.getBean("gpmWfFormService");
		GpmWfFormEntity wfFormEntity = gpmWfFormService.queryById(sourceid);
		GpmProjectHistoryService gpmProjectHistoryService = (GpmProjectHistoryService)SpringUtil.getBean("gpmProjectHistoryService");
		// 查找项目实体
		if(wfFormEntity == null) {
			return;
		}
		String projectid = ConvertUtil.convertToString(wfFormEntity.getSourceid());
		//查询项目entity
		GpmProjectEntity gpmProjectEntity =gpmProjectService.queryById(projectid);
		if(gpmProjectEntity == null) {
			return;
		}
		//更新字段 实际完成时间，提醒时间，进度状态和项目状态,将状态修改为任命状态
		gpmProjectEntity.setProjectstate(GpmSystemConst.SdProjectState.ProjectAppointment);
		Date now = new Date();
		gpmProjectEntity.setFinishdate(now);
		String remindAfterProject = ParameterCache.getValue("SDRemindAfterProject", "3");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		calendar.add(Calendar.DAY_OF_MONTH, ConvertUtil.convertToInt(remindAfterProject));
		gpmProjectEntity.setReminddate(calendar.getTime());
		gpmProjectEntity.setState(9);
		gpmProjectService.updateEntity(gpmProjectEntity);
		// 生成历史
	}

	@Transactional(rollbackFor = Exception.class)
	public void generateHistory(ITapService service, TapEntity entity) throws Exception {
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil
				.getBean("gpmProjectService");
		// 查找审批实体
		GpmWfFormService gpmWfFormService = (GpmWfFormService)SpringUtil.getBean("gpmWfFormService");
		GpmWfFormEntity wfFormEntity = gpmWfFormService.queryById(sourceid);
		GpmProjectHistoryService gpmProjectHistoryService = (GpmProjectHistoryService)SpringUtil.getBean("gpmProjectHistoryService");
		// 查找项目实体
		if(wfFormEntity == null) {
			return;
		}
		String projectid = ConvertUtil.convertToString(wfFormEntity.getSourceid());
		//查询项目entity
		GpmProjectEntity gpmProjectEntity =gpmProjectService.queryById(projectid);
		if(gpmProjectEntity == null) {
			return;
		}
		gpmProjectService.generateHistoryInfo(gpmProjectEntity.getProjectcode());
	}

	/**
	 * @param service
	 * @param entity
	 * @throws Exception
	 * @Title: startDemandFlow
	 * @description: 立项审批结束后开启需求流程
	 * @author: houyuhang
	 * @date: 2022年03月08日
	 * @modify:
	 */
	@Transactional
	public void startDemandFlow(ITapService service, TapEntity entity) throws Exception {
		// 查找立项审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmWfFormService gpmWfFormService = (GpmWfFormService) SpringUtil.getBean("gpmWfFormService");
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
		CboUserService cboUserService = (CboUserService) SpringUtil.getBean("cboUserService");
		CboDeptService cboDeptService = (CboDeptService) SpringUtil.getBean("cboDeptService");
		CboOrgService cboOrgService = (CboOrgService) SpringUtil.getBean("cboOrgService");
		GpmWfFormEntity gpmWfFormEntity = gpmWfFormService.queryById(sourceid);
		if(ObjectUtil.isEmpty(gpmWfFormEntity)) {
			return;
		}
		GpmProjectEntity gpmProjectEntity = gpmProjectService.queryById(ConvertUtil.convertToString(gpmWfFormEntity.getSourceid()));
		if(ObjectUtil.isEmpty(gpmProjectEntity)) {
			return;
		}
		// 将项目经理封装为SessionUserBean类型
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" code = '{0}' ", gpmProjectEntity.getProjectmanagercode());
		List<CboUserEntity> cboUserEntities = cboUserService.queryByWhere(sql.toString());
		sql.clear();
		sql.appendSingle(" code = '{0}' ", gpmProjectEntity.getDeptcode());
		List<CboDeptEntity> cboDeptEntities = cboDeptService.queryByWhere(sql.toString());
		sql.clear();
		sql.appendSingle(" code = '{0}' ", gpmProjectEntity.getOrgcode());
		List<CboOrgEntity> cboOrgEntities = cboOrgService.queryByWhere(sql.toString());
		SessionUserBean sessionUserBean = new SessionUserBean();
		if (CollectionUtil.isEmpty(cboUserEntities) || CollectionUtil.isEmpty(cboDeptEntities) || CollectionUtil.isEmpty(cboOrgEntities)) {
			sessionUserBean = gpmProjectService.getSessionUserBean();
		} else {
			sessionUserBean.setUser(cboUserEntities.get(0));
			sessionUserBean.setDept(cboDeptEntities.get(0));
			sessionUserBean.setWorkDept(cboDeptEntities.get(0));
			sessionUserBean.setOrg(cboOrgEntities.get(0));
			sessionUserBean.setWorkOrg(cboOrgEntities.get(0));
		}
	}
	/***
	 * @description: 省总审批通过后，给经营管理部SD领导发送消息（目前为马宾）
	 * @param service
	 * @param entity
	 * @return
	 * @author: jiangtao1
	 * @date: 2022/5/16 15:58
	 * @modify:
	 */
	@Transactional
	public void sendMessageToSdManager(ITapService service, TapEntity entity) throws Exception {
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmWfFormService gpmWfFormService = (GpmWfFormService) SpringUtil.getBean("gpmWfFormService");
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
		GpmWfFormEntity gpmWfFormEntity = gpmWfFormService.queryById(sourceid);
		if(ObjectUtil.isEmpty(gpmWfFormEntity)) {
			return;
		}
		GpmProjectEntity gpmProjectEntity = gpmProjectService.queryById(ConvertUtil.convertToString(gpmWfFormEntity.getSourceid()));
		if(ObjectUtil.isEmpty(gpmProjectEntity)) {
			return;
		}
		DatacodeCache datacodeCache = (DatacodeCache)SpringUtil.getBean("datacodeCache");
		String projectlevel = datacodeCache.getValueDynamic("GpmProjectLevel", gpmProjectEntity.getProjectlevel(), "code", "name");
		String usercodes = ParameterCache.getValue("GpmInformLeader","'0474'");
		gpmProjectService.sendMessagetoSdLeader(gpmProjectEntity.getProjectname(),projectlevel,usercodes, GpmProjectEntity.tableName, ConvertUtil.convertToLong(sourceid));
	}

}
